/**
 * 根据给定的地图范围计算场景的高度
 * @param {*} bounds 地图范围
 * @returns 场景高度
 */
export function calculateAltitudeFromBounds(bounds) {
  var _PI = 3.1415926;
  var _earthRadius = 6378137;
  var altitude = _earthRadius;
  var boundsWidth = bounds.right - bounds.left;
  if (boundsWidth >= 120) {
    altitude = _earthRadius * boundsWidth / 60 - _earthRadius;
  } else if (boundsWidth != 0) {
    var angle1 = (boundsWidth / 360) * _PI;
    var height = Math.sin(angle1) * _earthRadius;
    var a = height / Math.tan(angle1);
    var b = height / Math.tan(_PI / 6);
    altitude = a + b - _earthRadius;
  }
  return altitude;
}

/**
 * 根据场景高度计算地图中显示范围的宽度
 * @param {*} altitude 场景高度
 */
export function calculateSizeFromAltitude(altitude) {
  var _PI = 3.141592653;
  var _earthRadius = 6378137;
  var size;
  if (altitude >= _earthRadius)
  { //当场景高度大于可全幅显示整球的高度时
    var ratio = (altitude + _earthRadius) * 0.5;
    size = 120 * ratio / _earthRadius
  } else { //当场景高度小于可全幅显示整球的高度时，即无法看到整球时
    var tan30 = Math.tan(_PI / 6);
    //设置方程组的a,b,c
    var a = (Math.pow(tan30, 2) + 1) * Math.pow(_earthRadius, 2);
    var b = -2 * (_earthRadius + altitude) * _earthRadius * Math.pow(tan30, 2);
    var c = Math.pow(tan30, 2) * Math.pow(_earthRadius + altitude, 2) - Math.pow(_earthRadius, 2.0);
    //解一元二次方程，取锐角，因此余弦值较大
    var cosd = (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
    var d = Math.acos(cosd);
    var widthd = 2 * d * _earthRadius;
    size = (widthd / (_PI * _earthRadius)) * 180;
  }
  return size;
};